﻿using DevComponents.DotNetBar.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;

namespace ToolUtil
{
    public class FileTool
    {
        public static bool CheckFTP(string sServerIP,string uname,string pass,int port)
        {
            FtpWebRequest reqFTP;
            bool flag;
            try
            {
                // 根据uri创建FtpWebRequest对象 
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + sServerIP +":"+port+ "/"));
                if (reqFTP != null)
                {
                    // 请求对象创建成功
                    //reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; ;
                    //reqFTP.UseBinary = true;
                    // 可以设置其他FTP请求属性，比如Credentials，并执行请求
                    reqFTP.Credentials = new NetworkCredential(uname, pass);
                    // 执行请求...
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    // 处理响应流...

                    // 关闭流和响应对象
                    responseStream.Close();
                    response.Close();
                    flag = true;
                }
                else
                {
                    // 请求对象为null，创建失败
                    Console.WriteLine("FtpWebRequest创建失败");
                    flag = false;
                }                
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        public static void CopyFile(string FFileName, string EFileName)
        {
            int num = 0;
            int length = 0;
            byte[] buffer = new byte[0x400];
            if (File.Exists(FFileName))
            {
                Application.DoEvents();
                FileStream input = new FileStream(FFileName, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(input);
                reader.BaseStream.Seek(0L, SeekOrigin.Begin);
                length = (int)input.Length;
                if (File.Exists(EFileName))
                {
                    File.Delete(EFileName);
                }
                FileStream output = new FileStream(EFileName, FileMode.CreateNew, FileAccess.Write);
                BinaryWriter writer = new BinaryWriter(output);
                int count = 0x2710;
                while (count >= 0x400)
                {
                    count = reader.Read(buffer, 0, buffer.Length);
                    writer.Write(buffer, 0, count);
                    num += count;
                }
                output.Flush();
                input.Flush();
                writer.Close();
                reader.Close();
                output.Close();
                input.Close();
            }
            else
            {
                MessageBox.Show("源文件不存在请确认你的操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        public static void CopyAndDeleteFile(string FFileName, string EFileName)
        {
            int num = 0;
            int length = 0;
            byte[] buffer = new byte[0x400];
            if (File.Exists(FFileName))
            {
                Application.DoEvents();
                FileStream input = new FileStream(FFileName, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(input);
                reader.BaseStream.Seek(0L, SeekOrigin.Begin);
                length = (int)input.Length;
                if (File.Exists(EFileName))
                {
                    File.Delete(EFileName);
                }
                FileStream output = new FileStream(EFileName, FileMode.CreateNew, FileAccess.Write);
                BinaryWriter writer = new BinaryWriter(output);
                int count = 0x2710;
                while (count >= 0x400)
                {
                    count = reader.Read(buffer, 0, buffer.Length);
                    writer.Write(buffer, 0, count);
                    num += count;
                }
                output.Flush();
                input.Flush();
                writer.Close();
                reader.Close();
                output.Close();
                input.Close();
                File.Delete(FFileName);
            }
            else
            {
                MessageBox.Show("源文件不存在请确认你的操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        public static void CopyFilePro(string FFileName, string EFileName, ProgressBarX ProBar)
        {
            int num = 0;
            int length = 0;
            byte[] buffer = new byte[0x400];
            if (File.Exists(FFileName))
            {
                Application.DoEvents();
                FileStream input = new FileStream(FFileName, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(input);
                reader.BaseStream.Seek(0L, SeekOrigin.Begin);
                length = (int)input.Length;
                if (File.Exists(EFileName))
                {
                    File.Delete(EFileName);
                }
                FileStream output = new FileStream(EFileName, FileMode.CreateNew, FileAccess.Write);
                BinaryWriter writer = new BinaryWriter(output);
                int count = 0x2710;
                ProBar.Maximum = length;
                while (count >= 0x400)
                {
                    Application.DoEvents();
                    count = reader.Read(buffer, 0, buffer.Length);
                    writer.Write(buffer, 0, count);
                    num += count;
                    ProBar.Value = num;
                }
                output.Flush();
                input.Flush();
                writer.Close();
                reader.Close();
                output.Close();
                input.Close();
            }
            else
            {
                MessageBox.Show("源文件不存在请确认你的操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        // 递归遍历指定目录及子目录下的所有.xlsx文件，并剔除以~开头的文件
        public static List<string> TraverseDirectory(string directoryPath,string[] extensions)
        {
            List<string> xlsxFiles = new List<string>();
            foreach (string extension in extensions)
            {
                string[] files = Directory.GetFiles(directoryPath)
               .Where(file => !file.StartsWith("~") && Path.GetExtension(file).Equals(extension, StringComparison.OrdinalIgnoreCase))
               .ToArray();

                // 遍历当前目录下的所有.xlsx文件   new DirectoryInfo(directoryPath).GetFiles("*.xlsx")
                foreach (string file in files)
                {
                    // 将文件路径添加到列表中
                    xlsxFiles.Add(file);
                }
            }
            // 遍历当前目录下的所有子目录
            foreach (string subdirectory in Directory.GetDirectories(directoryPath))
            {
                // 递归调用遍历子目录，并将结果合并到列表中
                xlsxFiles.AddRange(TraverseDirectory(subdirectory, extensions));
            }

            return xlsxFiles;
        }
    }
}
